package webservice.restful;

import java.io.StringWriter;
import javax.ejb.EJB;
import javax.ws.rs.PathParam;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.json.XML;
import session.stateful.LoyaltyProgramSessionBean;
import session.stateful.ReservationSessionBean;
import util.helper.PointRecordList;
import util.helper.RedemptionListHelper;

@Path("redemptionJsonWebService")
public class RedemptionJsonWebService {

    @EJB
    ReservationSessionBean reservationSessionBean;
    @EJB
    LoyaltyProgramSessionBean loyaltyProgramSessionBean;

    public RedemptionJsonWebService() {
    }

    /*
     * input is tourist id and output is the point history of this tourist
     * For PointRecordList, there are two components:
     * a)List of ReservationHelper, which are the reservations that this tourist
     * has made (and reported as "Show") together with the point awarded. You can
     * retrieve the point received for this one by method getPointReceived();
     * b)List of RedemptionItemEntity, which are the redemptions that this tourist
     * has made, together with the points he spends for this item. You can retrieve
     * the point spent for this item by method getPointCost();
     */
    @GET
    @Produces("application/json")
    public String getPointHistoryJson(@PathParam("touristId") String touristId) {
        String jsonString = "";

        try {
            PointRecordList list = new PointRecordList();

            list.setReservationList(reservationSessionBean.getReservationHelperListByTouristId(Long.valueOf(touristId)));
            list.setRedemptionList(loyaltyProgramSessionBean.getRedemptionHelperListByTouristId(Long.valueOf(touristId)));
            
            JAXBContext jc = JAXBContext.newInstance(PointRecordList.class);
            Marshaller marshaller = jc.createMarshaller();
            StringWriter writer = new StringWriter();
            marshaller.marshal(list, writer);

            String xmlString = writer.toString();

            jsonString = XML.toJSONObject(xmlString).toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return jsonString;
    }

    /*
     * Purpose: input is touristId and output is list of redemption items that this
     * tourist has made
     * Methods:
     * getRedemption() returns RedemptionItemEntity, which only contains specific info
     * for this redemption
     * getRedemptionList() returns RedemptionListEntity, which has more info about 
     * this redemption.
     * getPointCost() returns point cost for this redemptionItem
     */
    @GET
    @Produces("application/json")
    public String getRedemptionListJson(@PathParam("touristId") String touristId) {
        String jsonString = "";

        try {

            RedemptionListHelper list = new RedemptionListHelper(); 
            list.setRedemptionList(loyaltyProgramSessionBean.getRedemptionHelperListByTouristId(Long.valueOf(touristId)));

            JAXBContext jc = JAXBContext.newInstance(RedemptionListHelper.class);
            Marshaller marshaller = jc.createMarshaller();
            StringWriter writer = new StringWriter();
            marshaller.marshal(list, writer);

            String xmlString = writer.toString();

            jsonString = XML.toJSONObject(xmlString).toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return jsonString;
    }
}
